/***************************************************************************
 创建者: 华磊
 开始时间: 2020.8.14
 copyright: (C) 华友高科
 修改说明: (每次有修改就添加一条，带有 修改人，修改时间，修改描述)
 example (1) hualei 2020.5.8 类的具体实现代码编写

 ***************************************************************************
 *  @file x.h
 *  功能:
 *
 *                                                                         *
 ***************************************************************************/
#ifndef MODBUSCOM_H
#define MODBUSCOM_H

//#include "modbusdriverconfig.h"
#include "ecatmasterbase.h"
#include "robotbase.h"
#include "modbus.h"

class ModbusDriverConfig;
class Modbus485Driver ;
class ModbusIoModuleDriver ;
/**
 * @brief The ModbusCom class modbus接口
 */
class ModbusCom: public EcatMasterBase
{
public:
    ModbusCom(E_MODBUS_TYPE busTypeIn, int busIdIn
              ,QList<QString> xmlFilelist,QList<int> deviceTypeList,QList<int> aliasList,
              int pthread_priority_in=40,int pthread_schedule_in=SCHED_RR);
    ~ModbusCom();
public:
    int getModbusDebugInfo(EthercatDebugInfo& debugInfo);
    int setAlias(int node, unsigned short alias){}
    int sdoUpload(int node
                  ,int rObIndex
                  ,int rObSubIndex
                  ,LONG64 &rData
                  ,int &rDataLen){}
    int sdoDownload(int node
                    , int wObIndex
                    , int wObSubIndex
                    , LONG64 wData
                    , int wDataLen){}


protected:
    void masterBootStrap(){}
    void masterError();
    void masterInit();
    void masterPreOp();
    void masterSafeOp(){}
    void masterOp();
    void updateDomainData();
    int readMotorData_modbus_rtu(RobotBase* rob);
    int readMotorData_modbus_tcp(RobotBase* rob);
    int writeMotorData_modbus_rtu(RobotBase* rob);
    int writeMotorData_modbus_tcp(RobotBase* rob);
    int readMotorData(RobotBase *rob);
    int readIoData(RobotBase *rob);
    int readIoData_modbus_rtu(RobotBase *rob);
    int readIoData_modbus_tcp(RobotBase *rob);
    int writeIoData_modbus_rtu(RobotBase *rob);
    int writeIoData_modbus_tcp(RobotBase *rob);
    int writeMotorData(RobotBase *rob);
    int writeIoData(RobotBase *rob);

private:
    int checkTimeMaxLimit();
    int updateControlMode();
    int initialEcatIoDidoCount();
    int initial(QList<QString> xmlFilelist, QList<int> deviceTypeList, QList<int> aliasList);
    /**
     * @brief addMsg  , 添加消息反馈
     */
    void addMsg( int messageLevel,
                 std::string componentName,
                 std::string messageType,
                 int messageCode,
                 int robotId,
                 int parameter1 = 0,
                 int parameter2 = 0,
                 int parameter3 = 0,
                 int parameter4 = 0 );


private:
    int currentAlias;//modbus-rtu用，当前通信的节点
    modbus_t* currentModbusMaster;//modbus-rtu用，当前通信的节点对应的modbus_t*

    QMap<int,Modbus485Driver *> motorDriverMap;
    QVector<Modbus485Driver *>motorDriver;
    QMap<int,ModbusIoModuleDriver *> ioDriverMap;
    QVector<ModbusIoModuleDriver*> ioDriver;
    E_MODBUS_TYPE busType;
    int busId;
    CommunicateParameter communicateParameter;


};

#endif // MODBUSCOM_H
